/************************************************************************************
* PartialCollectionModel.cpp                                                        *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "PartialCollectionModel.h"
#include <QFont>

PartialCollectionModel::PartialCollectionModel(QObject *parent) :
    QAbstractItemModel(parent)
{
//    m_items = NULL;
    m_items = new QList<CollectorItem *>;
}

PartialCollectionModel::~PartialCollectionModel() {
    if(m_items != NULL) {
        delete m_items;
    }
}


int PartialCollectionModel::rowCount(const QModelIndex &) const{
    return m_items->size();
}


int PartialCollectionModel::columnCount(const QModelIndex &) const {
    return 4;
}


QVariant PartialCollectionModel::data(const QModelIndex &index, int role) const {
    if(m_items == NULL || ! index.isValid() || index.row() >= m_items->size()) {
        return QVariant();
    }
    if (! index.isValid()) {
        return QVariant();
    }
    if (role == Qt::TextAlignmentRole) {
        return int (Qt::AlignLeft | Qt::AlignVCenter);
    }
    else if (role == Qt::DisplayRole) {
        CollectorItem* source = m_items->at(index.row());
        switch(index.column()) {
        case 0: return source->trackId();
        case 1: return source->title();
        case 2: return source->artist();
        case 3: return source->album();
        }
    }
    else if(role == Qt::ToolTipRole) {
        CollectorItem* source = m_items->at(index.row());
        QString tooltip = QString() +
                tr("Title  : <b>")+source->title()+"</b><br>"+
                tr("Artist : <b>")+source->artist()+"</b><br>"+
                tr("Album  : <b>")+source->album()+"</b>";
        return tooltip;
    }
    else if(role == Qt::SizeHintRole) {
        return QSize(0, 18);
    }
    else if(role == Qt::FontRole) {
        return QFont("Droid Sans", 9);
    }
    return QVariant();
}






QVariant PartialCollectionModel::headerData(int section, Qt::Orientation, int role) const {
    if (role == Qt::TextAlignmentRole) {
        return int (Qt::AlignLeft | Qt::AlignVCenter);
    }
    else if (role == Qt::DisplayRole) {
        switch(section) {
        case 0: return tr("UUID");
        case 1: return tr("Title");
        case 2: return tr("Artist");
        case 3: return tr("Album");
        }
    }
    return QVariant();
}


//It's not clear if the delete operation here is model's
//responsibility. Needs some analysis.
void PartialCollectionModel::setModel(QList<CollectorItem *> *items) {
    if(m_items != NULL && ! m_items->empty()) {
        m_items->clear();
    }
    QList<CollectorItem *> *temp = m_items;
    beginResetModel();
    m_items = items;
    endResetModel();
    delete temp;
}


void PartialCollectionModel::clear() {
        beginRemoveRows(QModelIndex(), 0, m_items->size() - 1);
        m_items->clear();
        endRemoveRows();
}


/*
  It is expected that whoever use this model responsible remove items from
  the list and set it for the model, since it is easy for them. Other wise
  it will be as messy as in the Collector wherein I use both list and a map.
 */
//void PartialCollectionModel::remove(QList<QString>& ids) {
//    //This is going to be ugly... Needs a better data structure...
//    //Or may be a better algorithm... but later
//    int removeIndex = 0;
//    while(removeIndex < ids.size()) {
//        for(int i = 0; i < ids.size(); i++) {
//            if(m_items->at(removeIndex)->uuid() == ids[i]) {
//                CollectorItem *item = m_items->at(removeIndex);

//            }
//        }
//    }


//    foreach(QString uuid, ids){
//        CollectorItem* item = m_itemMap->value(uuid);
//        item->setRemoved(true);
//        m_itemMap->remove(uuid);
//        delete item;
//    }
//    beginResetModel();

//    endResetModel();
//    YDb::get()->updater()->removeCollectionItems(&ids);
//}




QModelIndex PartialCollectionModel::index(int row, int column, const QModelIndex &) const {
    return createIndex(row, column);
}


QModelIndex PartialCollectionModel::parent(const QModelIndex &)  const {
    return QModelIndex();
}


bool PartialCollectionModel::hasChildren(const QModelIndex &parent) const {
    if(! parent.isValid()) {
        return true;
    }
    return false;
}


void PartialCollectionModel::append(CollectorItem *item) {
    if(m_items == NULL) {
        m_items = new QList<CollectorItem *>();
    }
    beginResetModel();
    m_items->append(item);
    endResetModel();
}

